//
//  Auth.swift
//  ShareUmbrella
//
//  Created by niupark on 2017/8/9.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//
//  Roger:18684909663

import UIKit
import RxSwift
import YSKit

let Auth = AuthManager.sharedInstance
let Defaults = UserDefaults.standard


public let SOSNotifcation = Notification.Name(rawValue: "ShareUmbrella.IFRecept")


class AuthManager: NSObject {
    static let sharedInstance = AuthManager()
    

    var token: String = ""
    var isLogin: Variable<Bool>! //是否登录
    private override init() {
        super.init()
        if let token = Defaults[DefaultsKey<String?>("token")], token != "" {
            self.isLogin = Variable(true)
        }else {
            self.isLogin = Variable(false)
        }

    }
    
    //登录
     @discardableResult
     func login(_ token: String? = nil) -> Bool{
        //
        if let t = token {
            self.token = t
            Defaults[DefaultsKey<String?>("token")] = self.token
            Defaults[DefaultsKey<String?>("phone")] = UserService.sharedInstance.phone.value
            Defaults[DefaultsKey<String?>("password")] = UserService.sharedInstance.password.value

        }else{
            self.token = Defaults[DefaultsKey<String?>("token")] ?? ""
        }
        if let token = Defaults[DefaultsKey<String?>("token")], token != "" {
            self.isLogin.value = true
        }else {
            self.isLogin.value = false
        }

        
        return self.isLogin.value
    }
    //退出登录。
    func logout(){
        //暂停定位
        self.token = ""
        Defaults[DefaultsKey<String?>("token")] = self.token
        self.isLogin.value = false
    }
    
    
}


extension AuthManager : AMapSearchDelegate{
    func onReGeocodeSearchDone(_ request: AMapReGeocodeSearchRequest!, response: AMapReGeocodeSearchResponse!) {
        
        if response.regeocode == nil {
            return
        }
        
        //解析response获取地址描述，具体解析见 Demo
    }
}

